﻿
/**
 * 
 **/

Ext.define('PT.view.window.EditCustomerWindow', {
    extend: 'Ext.window.Window',
    width: 600,
    height: 400,
    modal: true,
    rec: null,
    title: '客户编辑',
    initComponent: function () {

        var me = this;
        var cityCodeStore = Ext.create('PT.store.CityCodeStore');

        var from = Ext.create('Ext.form.Panel', {
            region: 'center',
            bodyPadding: 5,
            layout: 'anchor',
            fieldDefaults: {
                labelAlign: 'right'
            },
            defaults: {
                anchor: '95%'
            },
            defaultType: 'textfield',
            items: [{
                fieldLabel: '姓名',
                name: 'name',
                anchor: '80%',
                allowBlank: false
            },
                {
                    xtype: 'fieldcontainer',
                    defaultType: 'textfield',
                    anchor: '100%',
                    layout: 'hbox',
                    fieldDefaults: {
                        labelAlign: 'right'
                    },
                    items: [{
                        fieldLabel: '职位',
                        name: 'position',
                        allowBlank: false,
                        xtype: 'combobox',
                        store: Ext.create('PT.store.TaskType'),
                        queryMode: 'local',
                        displayField: 'name',
                        valueField: 'value',
                        anchor: '50%'
                    }, {
                        xtype: 'fieldcontainer',
                        fieldLabel: '性别',
                        defaultType: 'radiofield',                        
                        layout: 'hbox',
                        anchor: '50%',
                        items: [
                            {
                                boxLabel: '先生',
                                name: 'sex',
                                inputValue: '1',
                                checked: true,
                                id: 'sex_radio1'
                            }, {
                                boxLabel: '女士',
                                name: 'sex',
                                inputValue: '0',
                                id: 'sex_radio2'
                            }
                        ]
                    }]
                }, {
                    xtype: 'fieldcontainer',
                    defaultType: 'textfield',
                    anchor: '100%',
                    layout: 'hbox',
                    fieldDefaults: {
                        labelAlign: 'right',
                        anchor: '50%'
                    },
                    items: [{
                        fieldLabel: '手机号码',
                        name: 'mobile',
                        maxLength: 15,
                        allowBlank: false
                    }, {
                        fieldLabel: '座机',
                        name: 'tele',
                        maxLength: 15
                    }]
                },
                {
                    xtype: 'fieldcontainer',
                    defaultType: 'textfield',
                    anchor: '100%',
                    layout: 'hbox',
                    fieldDefaults: {
                        labelAlign: 'right',
                        anchor: '50%'
                    },
                    items: [{
                        fieldLabel: 'qq',
                        name: 'qq',
                        maxLength: 20,
                        allowBlank: true,                      
                    }, {
                        fieldLabel: '邮箱',
                        name: 'email',
                        vtype:'email'                        
                    }]
                },
				{
					    xtype: 'fieldcontainer',
					    defaultType: 'textfield',
					    anchor: '100%',
					    layout: 'hbox',
					    fieldDefaults: {
					        labelAlign: 'right'					        
					    },
					    items: [{
					        fieldLabel: '省份',
					        name: 'province',
					        xtype: 'combobox',
					        store: Ext.create('PT.store.ProvCodeStore'),
                            queryMode: 'local',
                            displayField: 'value',
                            valueField: 'key',                            
                            editable:false,        				
                            listeners:{
                                'change':function(m, newValue, oldValue,  eOpts ){
	                								
                                    var form= from.getForm();
                                    form.findField('city').setValue('');
	                							
                                    cityCodeStore.clearFilter(true);
                                    cityCodeStore.filter([
                                        {property: "refcode", value: newValue}
                                    ]);
                                }
                            }
                      }	,{
    				    fieldLabel: '城市',
    				    xtype: 'textfield',
    				    name: 'city',
    				    xtype: 'combobox',
    				    store: cityCodeStore,
    				    queryMode: 'local',    				   
    				    displayField: 'value',
    				    editable:false,
    				    valueField: 'key'
    				}]
					}, {
    				    fieldLabel: '可见度',
    				    xtype: 'textfield',
    				    name: 'privilege',
    				    maxLength: 512,
    				    anchor: '50%'
					}, {
					    fieldLabel: '备注',
					    xtype: 'textareafield',
					    name: 'remarks',                        
					    maxLength: 512,
					    anchor: '90%'
					}, {					   
					    xtype: 'hiddenfield',
					    name: 'id'
					}]
        });

        Ext.applyIf(me, {
            layout: 'border',
            items: [from],
            dockedItems: [{
                xtype: 'toolbar',
                dock: 'top',
                items: [{
                    text: '保存',
                    tooltip: '保存编辑信息',
                    iconCls: 'add',
                    handler: function () {

                        var form = me.down('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'Customer/Create',
                                success: function (form, action) {
                                    Ext.Msg.alert('Success', action.result.msg);

                                    me.close();
                                },
                                failure: function (form, action) {
                                    Ext.Msg.alert('Failed', action.result.msg);
                                }
                            });
                        }

                    }
                }, {
                    text: '关闭',
                    tooltip: '关闭窗口',
                    iconCls: 'ok',
                    handler: function () {
                        me.close();
                    }
                }]
            }]
        });

        me.callParent(arguments);
        me.on('beforerender', me.on_beforerender);

    }, on_beforerender: function (me, eOpts) {

        var rec = me.rec;

        if (rec != null) {

            var form = me.child('form').getForm();

            form.loadRecord(rec);
        }
    }
});